/**  
 * All rights Reserved, Designed By www.woniuxy.com
 * @Title:  Account.java   
 * @Package com.woniu.domain   
 * @Description:    Description  
 * @author: 蜗牛学院  
 * @date:   2019年8月22日 下午5:49:54
 * @user:   Administrator  
 * @version V1.0 
 * @Copyright: 2019 www.woniuxy.com Inc. All rights reserved. 
 * 注意：本内容仅限于成都蜗牛创想科技有限公司，禁止外泄以及用于其他的商业目
 */ 
package com.woniu.ssh.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.woniu.ssh.domain.Animal;
import com.woniu.ssh.domain.Category;
import com.woniu.ssh.domain.Distribution;
import com.woniu.ssh.utils.PageBean;
import com.woniu.ssh.utils.Queryv;

/**
 * @author Administrator
 *
 */
@Repository
public class AnimalDaoImpl implements AnimalDao{
	@Autowired
	SessionFactory sf;
	@Override
	public List<Animal> findAnimalList() {
		String hql=" from Animal";
		return sf.getCurrentSession().createQuery(hql).list();
	}
	@Override
	public List<Animal> findAnimalLists(Queryv query,PageBean pb) {
		String hql ="from Animal a where 1=1";
		if(query != null) {
			if(query.getAname() != null && !"".equals(query.getAname())) {
				hql += " and a.aname like '%"+query.getAname()+"%'";
			}
			if(query.getCid() != 0) {
				hql += " and a.category.cid = "+query.getCid();
			}
			if(query.getDid() != 0) {
				hql += " and a.distribution.did = "+query.getDid();
			}
		}
		return sf.getCurrentSession().createQuery(hql).setMaxResults(pb.getPageSize()).setFirstResult(pb.getStartRow()).list();
	}
	@Override
	public int AnimalCount(Queryv query) {
		String hql = " select count(*) from Animal a where 1=1";
		if(query != null) {
			if(query.getAname() != null && !"".equals(query.getAname())) {
				hql += " and a.aname like '%"+query.getAname()+"%'";
			}
			if(query.getCid() != 0) {
				hql += " and a.category.cid = "+query.getCid();
			}
			if(query.getDid() != 0) {
				hql += " and a.distribution.did = "+query.getDid();
			}
		}
		return ((Long)sf.getCurrentSession().createQuery(hql).uniqueResult()).intValue();
	}
	@Override
	public List<Category> findAllCategory() {
		String hql="from Category";
		return sf.getCurrentSession().createQuery(hql).list();
	}
	@Override
	public List<Distribution> findAllDistibution() {
		String hql="from Distribution";
		return sf.getCurrentSession().createQuery(hql).list();
	}
	@Override
	public void insertAnimal(Animal animal) {
		 sf.getCurrentSession().saveOrUpdate(animal);
	}
	@Override
	public Animal findAnimalById(int aid) {
		return (Animal) sf.getCurrentSession().get(Animal.class, aid);
	}

}
